package com.axapoint.opoc.core.dao;

import java.util.Collection;
import java.util.LinkedList;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Cascade;
import org.joda.time.DateTime;
import org.joda.time.Interval;

import com.anasoft.os.daofusion.bitemporal.WrappedBitemporalProperty;
import com.anasoft.os.daofusion.bitemporal.WrappedValueAccessor;

@Entity
@Table(name = "UnitLocation")
public class UnitLocation extends ObjectRegistration<UnitLocation, UnitLocation.Value, UnitLocation.Bitemporal> {
	private static final long serialVersionUID = 9168710745762860711L;

	@Entity
	@Table(name = "UnitLocationValues")
	public static final class Value extends _Value<UnitLocation, Bitemporal> {
		private static final long serialVersionUID = 1L;

		private long locationNo;
		private String pNo;
		private String openingHours;
		
		@SuppressWarnings("deprecation")
		@ManyToOne(fetch = FetchType.LAZY)
	    @Cascade(value = { org.hibernate.annotations.CascadeType.ALL, org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
	    @JoinColumn(name="locationId")
		private Location location;

		@SuppressWarnings("deprecation")
		@ManyToOne(fetch = FetchType.LAZY)
	    @Cascade(value = { org.hibernate.annotations.CascadeType.ALL, org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
	    @JoinColumn(name="unitId")
		private Unit unit;
				
		protected Value() {};
		Value(UnitLocation ul, String systemId, String userId, Operation operation, String comment, Value value) {
			super(ul, systemId, userId, operation, comment);
			this.unit = value.unit;
			this.location = value.location;
			this.openingHours = value.openingHours;
			this.pNo = value.pNo;
		}
		public Value(Unit unit, Location location, String openingHours, String pNo) {
			super();
			this.unit = unit;
			this.location = location;
			this.openingHours = openingHours;
			this.pNo = pNo;
		}
		@Override
		public String diff(_Value<UnitLocation, Bitemporal> otherValue, DateTime t0, DateTime t1) {
			StringBuffer sb = new StringBuffer();
			Value other = (Value)otherValue;
			
			if (pNo != null && !pNo.equals(other.pNo)) {
				sb.append("UnitLocation.pNo: ").append(pNo).append(" - ").append(other.pNo).append('\n');
			}
			if (openingHours != null && !openingHours.equals(other.openingHours)) {
				sb.append("UnitLocation.openingHours: ").append(openingHours).append(" - ").append(other.openingHours).append('\n');
			}
			if (locationNo != other.locationNo) {
				sb.append("UnitLocation.openingHours: ").append(locationNo).append(" - ").append(other.locationNo).append('\n');
			}

			Location.Bitemporal l0 = getLocation().getValues().get(t0);
			Location.Bitemporal l1 = getLocation().getValues().get(t1);

			if (!l0.getValue().getEntity().equals(l1.getValue().getEntity()) ||
				!l0.getValue().equals(l1.getValue())) {
				sb.append("UnitLocation.location: ");
				sb.append('[').append(l0.getValue().getEntity().getUuid()).append("] ").append(l0.getValue().getAddress()).append(" - ");
				sb.append('[').append(l0.getValue().getEntity().getUuid()).append("] ").append(l1.getValue().getAddress()).append('\n');
			}
			Unit.Bitemporal u0 = getUnit().getValues().get(t0);
			Unit.Bitemporal u1 = getUnit().getValues().get(t1);

			if (!u0.getValue().getEntity().equals(u1.getValue().getEntity()) ||
				!u0.getValue().equals(u1.getValue())) {
				sb.append("UnitLocation.unit: ");
				sb.append('[').append(u0.getValue().getEntity().getUuid()).append("] ").append(u0.getValue().getName()).append(" - ");
				sb.append('[').append(u0.getValue().getEntity().getUuid()).append("] ").append(u1.getValue().getName()).append('\n');
			}
			
			return sb.toString();
		}
		@Override
		public String toString() {
			StringBuffer sb = new StringBuffer();
			sb.append(super.toString());
			sb.append("  pNo=").append(pNo).append('\n');
			sb.append("  openingHours=").append(openingHours).append('\n');
			sb.append("  locationNo=").append(locationNo).append('\n');
			sb.append("  location=").append(location.getValues().get(getBitemporal().getValidityInterval().getStart()).getValue().getAddress()).append('\n');
			sb.append("  unit=").append(unit.getValues().get(getBitemporal().getValidityInterval().getStart()).getValue().getName()).append('\n');
			
			return sb.toString();
		}
		public long getLocationNo() {
			return locationNo;
		}
		protected void setLocationNo(long locationNo) {
			this.locationNo = locationNo;
		}
		public String getpNo() {
			return pNo;
		}
		protected void setpNo(String pNo) {
			this.pNo = pNo;
		}
		public String getOpeningHours() {
			return openingHours;
		}
		protected void setOpeningHours(String openingHours) {
			this.openingHours = openingHours;
		}
		public Location getLocation() {
			return location;
		}
		protected void setLocation(Location location) {
			this.location = location;
		}
		public Unit getUnit() {
			return unit;
		}
		protected void setUnit(Unit unit) {
			this.unit = unit;
		}
	}
	
	@Entity
	@Table(name = "UnitLocationBitemporal")
	public static final class Bitemporal extends _Bitemporal<Bitemporal, Value> {
		private static final long serialVersionUID = -2132187534862370208L;
		
		protected Bitemporal() {}
	    private Bitemporal(UnitLocation.Value value, Interval validityInterval) {
	        super(value, validityInterval);
	    }
	    
	    @Override
	    public Bitemporal copyWith(Interval validityInterval) {
	    	return new Bitemporal(value, validityInterval);
	    }
	}

	@SuppressWarnings("deprecation")
	@OneToMany(fetch = FetchType.LAZY)
    @Cascade(value = { org.hibernate.annotations.CascadeType.ALL, org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
    @JoinColumn(name="organizationChannelId")
    private Collection<OrganizationChannel.Value> organizationChannels = new LinkedList<OrganizationChannel.Value>();	

	@SuppressWarnings("deprecation")
	@OneToMany(fetch = FetchType.LAZY)
    @Cascade(value = { org.hibernate.annotations.CascadeType.ALL, org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
    @JoinColumn(name="engagementId")
    private Collection<Engagement.Value> engagements = new LinkedList<Engagement.Value>();	

	public UnitLocation() {
		super();
	}
    public Collection<OrganizationChannel.Value> getOrganizationChannels() {
		return organizationChannels;
	}
	protected void setOrganizationChannels(
			Collection<OrganizationChannel.Value> organizationChannels) {
		this.organizationChannels = organizationChannels;
	}
	public Collection<Engagement.Value> getEngagements() {
		return engagements;
	}
	protected void setEngagements(Collection<Engagement.Value> engagements) {
		this.engagements = engagements;
	}
	public WrappedBitemporalProperty<Value, Bitemporal> getValues() {
    	return new WrappedBitemporalProperty<Value, Bitemporal>(values, new WrappedValueAccessor<Value,Bitemporal>() {
			private static final long serialVersionUID = 1L;

			@Override
			public Bitemporal wrapValue(Value value, Interval validityInterval) {
				Bitemporal bi = new Bitemporal(value, validityInterval);
				return bi;
			}
    	});
    }
}
